package com.longtxt.core.ble.util

import android.app.Notification
import android.content.Intent
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification

class PowerbandNotifyService : NotificationListenerService() {
    companion object {
        const val QQ_PACK_NAME = "com.tencent.mobileqq";
        const val WECHAT_PACK_NAME = "com.tencent.mm";
        const val FACEBOOK_PACK_NAME = "com.facebook.katana";
        const val TALK_PACK_NAME = "com.kakao.talk";
        const val INSTAGRAM_PACK_NAME = "com.instagram.android";
        const val LINE_PACK_NAME = "jp.naver.line.android";
        const val WHATSAPP_PACK_NAME = "com.whatsapp";
        const val TWITTER_PACK_NAME = "com.twitter.android";
        const val SKYPE_PACK_NAME = "com.skype";
        const val TIKTOK_PACK_NAME = "com.youtube.android";
        const val SNAP_CHAT_PACK_NAME = "com.youtube.android";
        const val MINE_PACK_NAME = "com.realsil.android.powerband"
        const val NOTIFY_CODE = "notify_code"
        const val NOTIFY_TEXT = "notify_text"
        const val NOTIFY_TITLE = "notify_title"
        const val SMS_PACK_NAME = "smart_action_sms"
        const val PHONE_TYPE_PACK_NAME = "smart_action_phone"
        const val PHONE_STATE_TYPE = "smart_phone_type"
        const val MY_NOTIFY = "smart_my_notify"
        const val TAG = "PowerbandNotifyService"
    }

    private var notificationWhiteList: List<String> = listOf(
        QQ_PACK_NAME,
        WECHAT_PACK_NAME,
        FACEBOOK_PACK_NAME,
        TALK_PACK_NAME,
        INSTAGRAM_PACK_NAME,
        LINE_PACK_NAME,
        WHATSAPP_PACK_NAME,
        TWITTER_PACK_NAME,
        SKYPE_PACK_NAME,
        TIKTOK_PACK_NAME,
        SNAP_CHAT_PACK_NAME
    )


    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        if (sbn == null) {
            return
        }
       sbn.packageName?.let {pack ->
           val target = notificationWhiteList.firstOrNull { it == pack }
           if (target != null) {
               val extra = sbn.notification.extras
               val intent = Intent(MINE_PACK_NAME).apply {
                   action = MY_NOTIFY
                   putExtra(NOTIFY_CODE, pack)
                   putExtra(NOTIFY_TITLE, extra.getString(Notification.EXTRA_TITLE))
                   putExtra(NOTIFY_TEXT, extra.getString(Notification.EXTRA_TEXT))
               }
               sendBroadcast(intent)
           }
       }
    }

    override fun onCreate() {
        super.onCreate()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }
}